/**
 * ZeroNullNumberFormatter
 *
 * @author Chris Pratt
 *
 * 1/24/2012
 */
package com.anodyzed.onyx.text;

import com.anodyzed.onyx.log.Log;
import com.anodyzed.onyx.log.LogBuilder;

public class ZeroNullNumberFormatter extends NumberFormatter {
  private static final Log log = LogBuilder.getLogger();

  /**
   * Constructor
   *
   * @param parent The parent TextFormat
   * @param key The Argument Index or Map Key for this node 
   * @param bean The additional Bean reference String 
   */
  public ZeroNullNumberFormatter (TextFormat parent,CharSequence key,CharSequence bean) {
    super(parent,key,bean);
  } //ZeroNullNumberFormatter

  /**
   * Constructor
   *  
   * @param parent The parent TextFormat 
   * @param key The Argument Index or Map Key for this node 
   * @param bean The additional Bean reference String 
   * @param style The Date Style
   */
  public ZeroNullNumberFormatter (TextFormat parent,CharSequence key,CharSequence bean,CharSequence style) {
    super(parent,key,bean,style);
  } //ZeroNullNumberFormatter

  /**
   * Return a string representation of a numeric object
   *
   * @param args The Formatting Arguments
   * @return The Formatted String
   */
  @Override
  public String toString (Object... args) {
    Object arg = getValue(args);
    if(arg == null) {
      if(fraction || duration) {
        return "0";
      } else {
        try {
          return fmt.format(0);
        } catch(IllegalArgumentException x) {
          log.debug("Unable to format {0} ({0.class.name}) as a Number using ({1})",arg,fmt);
        }
      }
    }
    return super.toString(args);
  } //toString

} //*ZeroNullNumberFormatter
